package handler

import (
	"context"
	"fmt"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"rpc_demo/goods_srv/global"
	"rpc_demo/goods_srv/model"
	"rpc_demo/goods_srv/proto"
)

//func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
//	return func(db *gorm.DB) *gorm.DB {
//
//		if page <= 0 {
//			page = 1
//		}
//		switch {
//		case pageSize > 100:
//			pageSize = 100
//		case pageSize <= 0:
//			pageSize = 10
//		}
//
//		offset := (page - 1) * pageSize
//		return db.Offset(offset).Limit(pageSize)
//	}
//}

type GoodSrv struct {
}

func ModelToRes(goods *model.Goods) *proto.GoodsInfoResponse {
	goodsInfoResp := proto.GoodsInfoResponse{
		Id:         goods.ID,
		Name:       goods.Name,
		CategoryId: goods.CategoryId,
		BrandId:    goods.BrandsId,
	}
	return &goodsInfoResp
}

func (s *GoodSrv) GoodsList(ctx context.Context, req *proto.GoodsFilterRequest) (*proto.GoodsListResponse, error) {

	goodsListResponse := &proto.GoodsListResponse{}

	var goods []model.Goods

	queryDb := global.Db
	var subCateQuery string
	if req.Keywords != "" {
		fmt.Println(req.Keywords)
		queryDb = queryDb.Where("name like ? ", "%"+req.Keywords+"%")
	}
	if req.IsHost {
		queryDb = queryDb.Where(&model.Goods{IsHot: true})
	}
	if req.IsNew {
		queryDb = queryDb.Where(&model.Goods{IsNew: true})
	}
	//todo 品牌搜索

	//分类查询
	if req.TopCategory > 0 {
		var category model.Category
		if result := global.Db.First(&category, req.TopCategory); result.RowsAffected == 0 {
			return nil, status.Errorf(codes.NotFound, "商品分类不存在")
		}
		//一级分类
		if category.Level == 1 {
			subCateQuery = fmt.Sprintf("select id from category where parent_category_id %s", req.TopCategory)
		} else if category.Level == 1 {
			subCateQuery = fmt.Sprintf("select id from category where parent_category_id %s", req.TopCategory)
		} else if category.Level == 3 {
			subCateQuery = fmt.Sprintf("select id from category where parent_category_id %s", req.TopCategory)
		}
		queryDb = queryDb.Where(fmt.Sprintf("category_id in (?)", subCateQuery)).Find(&goods)
	}
	page := int(req.Page)
	pageSize := int(req.PagePerNums)

	queryDb.Preload("Category").Preload("Brands").Scopes(Paginate(page, pageSize)).Find(&goods)

	for _, good := range goods {
		goodsRes := ModelToRes(&good)
		goodsListResponse.Data = append(goodsListResponse.Data, goodsRes)
	}
	return goodsListResponse, nil

}
func (s *GoodSrv) BatchGetGoods(ctx context.Context, req *proto.BatchGoodsIdInfoRequest) (*proto.GoodsListResponse, error) {
	return &proto.GoodsListResponse{}, nil
}
func (s *GoodSrv) DeleteGoods(ctx context.Context, req *proto.DeleteGoodsIdInfoRequest) (*emptypb.Empty, error) {
	return &emptypb.Empty{}, nil
}
func (s *GoodSrv) UpdateGoods(ctx context.Context, req *proto.CreateGoodsInfoRequest) (*emptypb.Empty, error) {
	return &emptypb.Empty{}, nil
}
func (s *GoodSrv) GetGoodsDetial(ctx context.Context, req *proto.GoodsInfoRequest) (*proto.GoodsInfoResponse, error) {
	return &proto.GoodsInfoResponse{}, nil
}
